<?php
	function getDateBeforeToday()
	{
		$dt = explode("-", (new DateTime())->format('m-d-Y'));
		$dt[1] = (int) $dt[1] - 2; // set the date two days before today's date
		return $dt; // datetime
	}
		
	function userHasTakenTodaysQuiz($username)
	{
		if(isset($_POST['submitAnswer'])):
			return false;
		endif;
		$userLastQuizDate = getLastQuizDate($username);
		$userIsTakingQuiz = false;
		
	
			
		// if($userIsTakingQuiz && isset($_SESSION['lastQuizStartDate'])):
			// if(!$_SESSION['lastQuizStartDate'])
			// {
				// return true;
			// }
		// endif;
		// echo "<br/>Dumping if user has taken quiz...<br />";
		// var_dump($userLastQuizDate);
		if(isset($userLastQuizDate)):
			if(datetimeOccurredBeforeToday($userLastQuizDate)):
				return false;
			else:
				return true;
			endif;
		else:
		/* we could add a date to the cookie here. Not necessary. Will do when quiz starts. */
			return false;
		endif;
	}
	
	function getLastQuizDate($username)
	{
		$userLastQuizDate = getValueFrom2DcookieArray($username,"lastQuiz");
		if(isset($userLastQuizDate)):
			/* Note: cookie is stored m-d-Y */
			$userLastQuizDate = explode("-", $userLastQuizDate);
			/* But this constructor requires d-m-Y */
			$userLastQuizDate = new DateTime($userLastQuizDate[1] . "-" . $userLastQuizDate[0] . "-" . $userLastQuizDate[2]);
			return $userLastQuizDate;
		else:
			return null;
		endif;
	}
	
	function getValueFrom2DcookieArray($row, $column)
	{
		$row = $_COOKIE[$row];
		if(isset($row)):
			foreach($row as $key => $value):
				$key = str_replace("'", "", $key);
				$value = str_replace("'", "", $value);
				if(strcmp(sanitizeString($column), sanitizeString($key)) == 0):
					return $value;
				else:					
					continue;
				endif;
			endforeach;
		else:
			return null;
		endif;
		return null;
	}
	
	function datetimeOccurredBeforeToday($dt)
	{
		$todaysDate = new DateTime();
		if($dt->format("Y-m-d") < $todaysDate->format("Y-m-d")):
			return true;
		else:
			return false;
		endif;
	}
	
	function displayMessageThatUserHasAttemptedTodaysQuiz()
	{
		echo "<p>You have already taken today's quiz. Come back later!</p>";
	}
	
	function displayMessageThatUserHasNotAttemptedTodaysQuiz()
	{
		?>
			<form action="/proj1/quiz/quiz.php" method="post">
				<p>You have not taken today's quiz. 
					<input type="submit" value="Take Quiz" name="userWantsToTakeQuiz"> 
				</p>
			</form>
		<?php
	}
?>